package main.flex.renderer
{
	import flash.events.SyncEvent;
	import flash.media.Camera;
	import flash.media.Video;
	import flash.net.NetStream;
	import flash.net.SharedObject;
	
	import main.flex.connectivity.NetConnectionManager;
	
	import mx.core.IDataRenderer;
	import mx.core.UIComponent;
	import mx.events.FlexEvent;

	public class BaseVideoRenderer extends UIComponent implements IDataRenderer
	{
		// Internal variable for the property value.
	    private var _data		:	Object;
	    private var _cam		:	Camera;
	    private var _nc			:	NetConnectionManager = NetConnectionManager.getInstance();
	    private var _ns			:	NetStream;
	    private var _video		:	Video;
	    private var _sharedParam	:	SharedObject;

		public function BaseVideoRenderer( )
		{
			super();
		}
	    
	    // Make the data property bindable.
	    [Bindable("dataChange")]
	    // Define the getter method.
	    public function get data():Object {
	        return _data;
	    }
	    
	    // Define the setter method, and dispatch an event when the property
	    // changes to support data binding.
	    public function set data(value:Object):void
	    {
	    	_data = value;
	    
	        dispatchEvent(new FlexEvent(FlexEvent.DATA_CHANGE));
	    }
	    
	    private function init( pEvent:SyncEvent ):void
	    {
		    _cam = new Camera();
			_cam	= Camera.getCamera();
			_cam.setQuality( 0, 85 );
			_cam.setMode(320,240,18);
			_ns = new NetStream(_nc);
			_ns.attachCamera(_cam);
			_ns.publish( "cam" + sharedParam.data[0].userName ,"live" );
			var nsPlayer :	NetStream = new NetStream(_nc);
			nsPlayer.play( "cam" + sharedParam.data[0].userName );
			_video = new Video();
	    	_video.attachNetStream(nsPlayer);
	   		addChild(_video);
	   		invalidateDisplayList();
   		}
   		
   		[Bindable]
   		public function get sharedParam()	:	SharedObject
   		{
   			return _sharedParam;
   		}
   		
   		public function set sharedParam(pValue:SharedObject)	:void
   		{
   			_sharedParam =	pValue;
			/* sharedParam = SharedObject.getRemote("userList", _nc.uri, false); */
			sharedParam.addEventListener(SyncEvent.SYNC, init);
 		}
	}
}